package com.crowdsourcing.controller;

import com.crowdsourcing.model.Msg;
import com.crowdsourcing.model.Task;
import com.crowdsourcing.model.User;
import com.crowdsourcing.service.ITaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/receive")
public class ReceiveController {
    @Autowired
    HttpSession httpSession;
    @Autowired
    ITaskService service;

    /**
     * 所有任务
     * @param request
     * @return
     */
    @GetMapping("/all")
    public String all(HttpServletRequest request) {
        List<Task> tasks = service.allReceive();
        request.setAttribute("info", tasks);
        request.setAttribute("status", "all");
        return "receive";
    }

    /**
     * 查询完成的处理任务
     * @param request
     * @return
     */
    @GetMapping("/finish")
    public String finish(HttpServletRequest request) {
        User user = (User) httpSession.getAttribute("user");
        List<Task> tasks = service.finishReceive(user.getId());
        request.setAttribute("info", tasks);
        request.setAttribute("status", "finish");
        return "receive";
    }

    /**
     * 保存处理
     * @param taskId
     * @param content
     * @return
     */
    @ResponseBody
    @GetMapping("/save")
    public Msg all(Integer taskId, String content) {
        User user = (User) httpSession.getAttribute("user");
        service.saveReceive(taskId, content, user.getId());
        return Msg.success();
    }

    /**
     * 处理页面
     * @param taskId
     * @param request
     * @return
     */

    @GetMapping("/pull/{taskId}")
    public String pull(@PathVariable ("taskId") Integer taskId,HttpServletRequest request) {
        Task task=service.find(taskId);
        request.setAttribute("info", task);
        return "pull";
    }

    /**
     * 任务处理
     * @param taskId
     * @param request
     * @return
     */
    @GetMapping("/details/{taskId}")
    public String details(@PathVariable ("taskId") Integer taskId,HttpServletRequest request) {
        Task task=service.find(taskId);
        request.setAttribute("info", task);
        return "pull";
    }



}
